const express = require('express')
const { query } = require('../utils/pool')
const router = express.Router()

// 新闻分类
router.post('/cat/add', async (req, res, next) => {
  const obj = req.body
  try {
    const result = await query('insert into ih_cat set ?', [obj])
  } catch (err) {
    return next(err)
  }
  res.send({ code: 200, msg: '分类添加成功' })
})
router.get('/cat/list', async (req, res, next) => {
  try {
    var result = await query('select * from ih_cat')
  } catch (err) {
    return next(err)
  }
  res.send({ code: 200, msg: '新闻分类', data: result })
})
router.put('/cat/update', async (req, res) => {
  const obj = req.body
  try {
    var result = await query('update ih_cat set ? where id=?', [obj, obj.id])
  } catch (err) {
    return next(err)
  }
  console.log(result)
  if (result.changedRows === 0) {
    res.send({ code: 400, msg: '修改失败' })
  } else {
    res.send({ code: 200, msg: '修改成功' })
  }
})
router.delete('/cat', async (req, res) => {
  const obj = req.query
  const result = await query('delete from ih_cat where id=?', [obj.id])
  console.log(result)

  if (result.affectedRows === 0) {
    res.send({ code: 400, msg: '删除失败' })
  } else {
    res.send({ code: 200, msg: '删除成功' })
  }
})

// 文章
router.post('/article/add', async (req, res, next) => {
  const obj = req.body
  obj.ctime = Date.now()
  try {
    const result = await query('insert into ih_article set ?', [obj])
  } catch (err) {
    return next(err)
  }
  res.send({ code: 200, msg: '文章添加成功' })
})

// 文章列表
router.get('/article/list', async (req, res, next) => {
  const obj = req.query;
  if (!obj.pno) obj.pno = 1;
  if (!obj.count) obj.count = 10;
  const size = parseInt(obj.count);
  const start = (obj.pno - 1) * size;
  try {
    let sql
    if (obj.title || obj.cat_id) {
      let str = ''
      if (!obj.title) {
        str = `cat_id=${obj.cat_id}`
      } else if (!obj.cat_id) {
        str = `title like '%${obj.title}%'`
      } else {
        str = `title like '%${obj.title}%' and cat_id=${obj.cat_id}`
      }
      sql = `select aid,title,cat_id,cname,pic,author,detail,ctime,is_rec from ih_article,ih_cat where cat_id=id and (${str}) order by aid desc limit ?,?; select count(*) as n from ih_article where ${str}`
    } else {
      sql = 'select aid,title,cat_id,cname,pic,author,detail,ctime,is_rec from ih_article,ih_cat where cat_id=id order by aid desc limit ?,?; select count(*) as n from ih_article'
    }
    console.log(sql)
    var result = await query(
      sql,
      [start, size]
    )
  } catch (err) {
    return next(err)
  };
  console.log(result);
  res.send({
    code: 200,
    msg: '文章列表',
    data: result[0],
    total: result[1][0].n,
    pages: Math.ceil(result[1][0].n / size),
    pno: obj.pno
  });
});
// 删除文章
router.delete('/article', async (req, res) => {
  const obj = req.query
  const result = await query('delete from ih_article where aid=?', [obj.id])
  console.log(result)

  if (result.affectedRows === 0) {
    res.send({ code: 400, msg: '删除失败' })
  } else {
    res.send({ code: 200, msg: '删除成功' })
  }
})
// 修改文章
router.put('/article/update', async (req, res, next) => {
  const obj = req.body
  try {
    var result = await query('update ih_article set ? where aid=?', [obj, obj.aid])
  } catch (err) {
    return next(err)
  }
  console.log(result)
  if (result.changedRows === 0) {
    res.send({ code: 400, msg: '修改失败' })
  } else {
    res.send({ code: 200, msg: '修改成功' })
  }
})

// 搜索新闻
// router.get('/article/search',async (req, res, next) => {
//   const obj = req.query
//   try{
//     // const result = await query('select ')
//   }
// })


module.exports = router
